﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using YAPL.Language.Ast;
using Regexpr = System.Text.RegularExpressions.Regex;
namespace YAPL.Language.SourceParser.Helpers {
	public static class ConstantHelper {
		private const string STRING_PATTERN = "\'.*\'";
		private const string INTEGER_PATTERN = "\\d+";
		private const string FLOAT_PATTERN = "\\d+[.]\\d+";
		public static ConstantExpression ParseConstant(string value) {
			if (value == "true" || value == "false")
				return Expressions.Boolean(bool.Parse(value));
			if (Regexpr.IsMatch(value, STRING_PATTERN))
				return Expressions.StringLiteral(value.Substring(1,value.Length-2)); //remove quotation marks
			if (Regexpr.IsMatch(value, FLOAT_PATTERN))
				return Expressions.Float(double.Parse(value, new NumberFormatInfo()));
			if (Regexpr.IsMatch(value, INTEGER_PATTERN))
				return Expressions.Integer(int.Parse(value));
			throw new ArgumentException("Bad constant value");
		}
	}
}
